package main

import (
	"context"
	"flag"
	pb "gitee.com/go-programming-tour/gprc-demo/proto"
	"google.golang.org/grpc"
	"io"
	"log"
)

var port string

func init() {
	flag.StringVar(&port, "p", "8000", "启动端口号")
	flag.Parse()
}

func SayHello(client pb.GreeterClient) error {
	resp, _ := client.SayHello(context.Background(), &pb.HelloRequest{
		Name: "sdf",
	})
	log.Printf("client.Sayhello repo: %s", resp.Message)
	return nil
}

func Saylist(client pb.GreeterClient, r *pb.HelloRequest) error {
	stream, _ := client.SayList(context.Background(), r)
	for {
		resp, err := stream.Recv()
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}
		log.Printf("client.Saylist resp: %v", resp)
	}
	return nil
}

func SayRecord(client pb.GreeterClient, r *pb.HelloRequest) error {
	stream, _ := client.SayRecord(context.Background())
	for i := 0; i < 6; i++ {
		_ = stream.Send(r)
	}
	resp, _ := stream.CloseAndRecv()
	log.Printf("resq: %v", resp)
	return nil
}

func SayRoute(client pb.GreeterClient, r *pb.HelloRequest) error {
	stream, _ := client.SayRoute(context.Background())
	for i := 0; i < 6; i++ {
		_ = stream.Send(r)
		resp, err := stream.Recv()
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}
		log.Printf("resp :%v", resp)
	}
	_ = stream.CloseSend()
	return nil
}

func main() {
	conn, _ := grpc.Dial(":"+port, grpc.WithInsecure())
	defer conn.Close()

	client := pb.NewGreeterClient(conn)
	//_ = SayHello(client)
	//_ = Saylist(client, &pb.HelloRequest{Name: "sdf"})
	_ = SayRecord(client, &pb.HelloRequest{Name: "qwe"})
}
